<style scoped>
    .layui-row {
        overflow: hidden;
    }
    .select-item {
        width: 34.333333%;
        float: right;
    }
    .layui-row:after, .layui-row:before {
        clear: none;
    }
    .layui-row {
        overflow: hidden;
    }
    .select-item {
        width: 34.333333%;
        float: right;
    }
    .layui-row:after, .layui-row:before {
        clear: none;
    }
    .salesSpecification >>> .layui-form-select dl dd, .layui-form-select dl dt {
        text-align: left !important;
    }
</style>

<template>
    <div class="salesSpecification">
        <div>
            <template v-if="permission.permis.indexOf(38) > -1">
            <a href="javascript:;" class="layui-btn" @click="add"><i class="layui-icon">&#xe654;</i>添加合同</a>
            </template>
        </div>

        <form class="layui-form " >
            <div  class="layui-col-lg4 select-item" style="text-align: right;width: auto" >
                <div class="layui-input-inline" style="width: 190px">
                    <input type="text" ref="bname" placeholder="请输入合同编号/名称" class="layui-input key">
                </div>
                <div class="layui-inline" style="width: 160px">
                    <select ref="batchguid" lay-filter="batchguid" class="layui-form-select" lay-search>
                        <option value="">请选择批次</option>
                        <option v-for="(item, index) in batches" :value="item.guid">{{item.name}}</option>
                    </select>
                </div>
                <div class="layui-inline" lay-filter="partb" style="width: 160px">
                    <select ref="partb" class="layui-form-select" lay-search>
                        <option value="">请选择乙方</option>
                        <option v-for="(item, index) in plantors" :value="item.guid">{{item.realname}}</option>
                    </select>
                </div>
                <button type="button" class="layui-btn sou" @click="search">搜索</button>
            </div>

        </form>

        <table class="layui-table layui-form">

            <thead>
            <tr>
                <th>编号</th>
                <th>名称</th>
                <th>批次</th>
                <th>甲方</th>
<!--                <th>部门</th>-->
                <th>乙方</th>
                <th>地块</th>
                <th>产品</th>
                <th>规格</th>
                <th>单价(元/吨)</th>
                <!-- <th>包装重量(kg)</th>
                <th>质检标准</th>
                <th>质检方式</th> -->
                <th>扣杂方式</th>
                <th>扣杂率(%)</th>
                <!-- <th>土杂率(%)</th>
                <th>不合格率(%)</th> -->
                <th>创建人</th>
                <th>创建时间</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
                <tr v-for="(item, index) in listdata">
                    <td>{{item.code}}</td>
                    <td>{{item.name}}</td>
                    <td>{{item.bitchname}}</td>
                    <td>{{item.parta}}</td>
<!--                    <td>{{item.departname}}</td>-->
                    <td>{{item.partbname}}</td>
                    <td>{{item.arealandname}}</td>
                    <td>{{item.productname}}</td>
                    <td>{{item.specificationname}}</td>
                    <td>{{item.price}}</td>
                    <td>{{item.wghtperpkg}}</td>
                    <!-- <td>{{item.qaname}}</td>
                    <td>{{getQatypeName(item.qatype)}}</td>
                    <td>{{getDtypeName(item.deducttype)}}</td>
                    <td>{{item.discountrate}}</td> -->
                    <!-- <td>{{item.soilchkrate}}</td> -->
                    <td>{{item.unqualifiedchkrate}}</td>
                    <th>{{item.operator}}</th>
                    <td>{{item.createdate}}</td>
                    <template v-if="permission.permis.indexOf(38) > -1">
                    <td><a href="javascript:;" @click="edit(item.guid)">修改</a>
<!--                        | <a href="javascript:;" @click="del(val)">删除</a>-->
                    </td>
                    </template>
                </tr>
            </tbody>
        </table>

        <Pagination :cur="currentPage" :all="totalPage" :callback="turnPage" ></Pagination>
        <div style="height: 200px"></div>
    </div>
</template>

<script>
    import Pagination from '@/components/common/Pagination.vue'
    export default {
        name: 'ContractList',
        components: {Pagination},
        data () {
            return {
                listdata: [],
                currentPage: 1,
                totalPage: 0,
                plantors: [],
                batches: [],
                permission: JSON.parse(sessionStorage.getItem('permissions')),
            }
        },
        mounted() {
            let that = this;
            that.getPageData();
            that.getPlantors();
            that.getBatches();

            layui.use(['form'], function () {
                var form = layui.form,
                    layer = layui.layer;
            });

        },
        updated () {
            layui.form.render("select");
        },
        methods: {
            getDtypeName: function(val) {
                let name = '';
                switch (val) {
                    case 1:
                        name = '统一扣杂';
                        break;
                    case 2:
                        name = '分步扣杂';
                        break;
                    case 3:
                        name = '八三扣杂';
                        break;
                }
                return name;
            },
            getQatypeName: function(val) {
                let name = '';
                switch (val) {
                    case "1":
                        name = '库房质检';
                        break;
                    case "2":
                        name = '地头质检';
                        break;
                }
                return name;
            },
            getStypeName: function(val) {
                let name = '';
                switch (val) {
                    case "1":
                        name = '发货过磅';
                        break;
                    case "2":
                        name = '到货过磅';
                        break;
                }
                return name;
            },
            search: function() {
                this.getPageData('search');
            },
            turnPage: function(data){
                this.currentPage = data;
                this.getPageData();
            },
            getPageData: function(type = '') {
                let that = this;
                let loader = layui.layer.load();

                let postData = {};
                if (type != 'search') {
                    postData.pageNo = that.currentPage;
                }
                postData.search = this.$refs.bname.value;
                // postData.name = this.$refs.bname.value;
                postData.partb = this.$refs.partb.value;
                postData.batchguid = this.$refs.batchguid.value;
                postData.functiontype = 15;

                this.axios({
                    method: 'post',
                    url: '/harvest/Procurementcontract!list.action',
                    params: postData
                }).then((response) => {
                    layer.close(loader);
                    let res = response.data.pagelist;
                    if (response.data.msg == 'success') {
                        that.currentPage = res.pageNo;
                        that.totalPage = res.pageCount;

                        that.listdata = res.rows;
                    } else {
                        this.toastError("系统错误");
                    }

                })

            },
            getPlantors: function () {
                this.axios({
                    method: 'get',
                    url: '/harvest/Procurementcontract!getPlanters.action',
                    params: {}
                }).then(response => {
                    if (response.data.msg == 'success') {
                        this.plantors = response.data.planters;
                    } else {
                        this.toastText('获取乙方数据失败')
                    }
                });
            },
            getBatches: function() {
                this.axios({
                    method: 'get',
                    url: '/harvest/Procurementbatch!findBatchbyFun.action',
                    params: {status: 1, funtiontype: 15}
                }).then(response => {
                    if (response.data.batchs != null ) {
                        this.batches = response.data.batchs;
                        layui.form.render("select");
                    } else {
                        that.toast("获取批次数据失败");
                    }
                });
            },
            add: function () {
                this.$parent.switchPage('addContract');
            },
            edit: function (id) {
                this.$parent.gid = id;
                this.$parent.switchPage('editContract');
            }
        }
    }
</script>
